# -*- coding:utf-8 -*-
# !usr/bin/env python

import os
import sys
import pexpect

sys.path.append(os.path.join(sys.path[0],'../..'))


from csi_config import read_setting
wlan = read_setting('wlan')
PASSWD = read_setting('password')


if __name__ == '__main__':

    def error():
        print("Error - EOF!-- zz")
        sys.exit(-1)


    child = pexpect.spawn('sudo modprobe -r iwlwifi mac80211')
    try:
        index = child.expect('password')
        if index == 0:
            child.sendline(PASSWD)
    except pexpect.EOF:
        error()
    while child.isalive():
        pass
    child = pexpect.spawn('sudo modprobe iwlwifi connector_log=0x1')
    try:
        index = child.expect('password')
        if index == 0:
            child.sendline(PASSWD)
    except pexpect.EOF:
        error()
    while child.isalive():
        pass
    child = pexpect.spawn('sudo service network-manager stop')
    try:
        index = child.expect('password')
        if index == 0:
            child.sendline(PASSWD)
    except pexpect.EOF:
        error()
    while child.isalive():
        pass
    child = pexpect.spawn('sudo ip link set %s up' % wlan)
    try:
        index = child.expect('password')
        if index == 0:
            child.sendline(PASSWD)
    except pexpect.EOF:
        error()
    while child.isalive():
        pass
